package com.fearless.crm.workbench.service.impl;

import com.fearless.crm.utils.DateTimeUtil;
import com.fearless.crm.utils.UUIDUtil;
import com.fearless.crm.workbench.dao.CustomerDao;
import com.fearless.crm.workbench.dao.TranDao;
import com.fearless.crm.workbench.dao.TranHistoryDao;
import com.fearless.crm.workbench.entity.Customer;
import com.fearless.crm.workbench.entity.Tran;
import com.fearless.crm.workbench.entity.TranHistory;
import com.fearless.crm.workbench.service.TranService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TranServiceImpl implements TranService {

    @Resource
    private TranDao tranDao;

    @Resource
    private TranHistoryDao tranHistoryDao;

    @Resource
    private CustomerDao customerDao;


//添加交易,业务层方法
    public boolean saveTranService(Tran tran, String customerName) {
        boolean flag=true;
        /*
            交易添加操作。
                    参数tran里面少一项信息(customerId)
                    所以我们需要首先处理这个customerId
                1.根据客户名称customerName，在客户表进行精确查询，如果有结果，则取出这个客户的id，封装到
                tran中。如果没有结果，则新建一条这个客户信息,然后将新建的客户的id封装到tran中

                2.经过以上操作，tran里面信息就全了，现在我们进行交易的添加操作
                3.交易添加完毕后，创建一条交易历史
         */

            Customer customer=customerDao.getCustomerByName(customerName);

                if(customer==null){
                //没有结果,则新建一条这个客户信息,然后将新建的客户的id封装到tran中
                customer=new Customer();
                customer.setId(UUIDUtil.getUUID());
                customer.setCreateTime(DateTimeUtil.getSysTime());
                customer.setCreateBy(tran.getCreateBy());
                customer.setName(customerName);
                customer.setOwner(tran.getOwner());
                customer.setNextContactTime(tran.getNextContactTime());
                //添加客户
                int count= customerDao.saveNerCustomer(customer);

                if(count!=1){
                    flag=false;
                }

            }
        //通过上述的处理,不管是查询出来客户,还是我们新建的客户。总之客户已经有了，客户的id也有了
            tran.setCustomerId(customer.getId());//将客户的id，封装到tran中


            //添加交易。
            int count2=tranDao.saveTran(tran);
            if(count2!=1){
                flag=false;
            }

          //添加交易历史
            TranHistory tranHistory=new TranHistory();
            tranHistory.setId(UUIDUtil.getUUID());
            tranHistory.setTranId(tran.getId());
            tranHistory.setStage(tran.getStage());
            tranHistory.setMoney(tran.getMoney());
            tranHistory.setExpectedDate(tran.getExpectedDate());
            tranHistory.setCreateBy(tran.getCreateBy());
            tranHistory.setCreateTime(DateTimeUtil.getSysTime());
            //添加交易历史
        int count3=tranHistoryDao.saveTranHistory(tranHistory);
            if(count3!=1){
                flag=false;

            }

        return flag;
    }




//刷新交易列表
    public Map<String, Object> showTranListService(Tran tran) {

        System.out.println("!!!!!!!!!接受到的tran"+tran);


        //获取总的交易条数
        int total=tranDao.getTotalTran();

        //获取交易列表
        List<Tran> tranList=tranDao.getTranList(tran);


        Map<String,Object> map=new HashMap<>();
        map.put("total",total);
        map.put("tranList",tranList);
        return map;
    }




//跳转到详细页,根据id查单条
    public Tran getTranByIdService(String id) {

        Tran tran=tranDao.getTranByIdDao(id);
        return tran;
    }



//执行改变交易阶段的操作,业务层方法
    public boolean changeTranStage(Tran tran) {

        boolean flag=true;

        int count=tranDao.changeTranStageDao(tran);

        if(count!=1){
            flag=false;
        }


        //交易阶段改变后,生成一条交易历史
        TranHistory tranHistory=new TranHistory();
        tranHistory.setId(UUIDUtil.getUUID());
        tranHistory.setStage(tran.getStage());
        tranHistory.setMoney(tran.getMoney());
        tranHistory.setExpectedDate(tran.getExpectedDate());
        tranHistory.setCreateTime(DateTimeUtil.getSysTime());
        tranHistory.setCreateBy(tran.getEditBy());
        tranHistory.setTranId(tran.getId());
        //添加交易历史
        int count2=tranHistoryDao.saveTranHistory(tranHistory);

        if(count2!=1){
            flag=false;
        }

        return flag;
    }




//漏斗图展现,业务层方法
    public Map<String, Object> getChartsService() {

        //取得total
        int total=tranDao.getTotalTran();

        //取得dataList
        List<Map<String,Object>> dataList=tranDao.getCharts();

        //将total和dataList保存到map中

        Map<String,Object> map=new HashMap<>();
        map.put("total",total);
        map.put("dataList",dataList);


        //将map返回



        return map;
    }



//展现交易列表
    public List<Tran> getTransactionList(String id) {
        List<Tran> tranList=tranDao.getTransactionList(id);





        return tranList;
    }
}
